'use strict';

var Promise = require('/promise.js');
var project = require('../conf.js');
var apiDomain = project.domain;
var loginUrl = project.loginCgi;
var updateInfoCgi = project.updateInfoCgi;

var isbeta = (project.domain.indexOf('betamina') != -1);//是否为beta环境

function gotoLogin(resolve){
    wx.login({
        success: function (res) {
            if(res.code){
                var data = {
                    code: res.code
                }
                console.log(res.code)
                if(isbeta) {// 测试环境
                    loginUrl = project.domain + '/wxapp_api/api'
                    data.action = '/wxapp_api/user/login'
                }
                wx.request({
                    url: loginUrl,
                    method: 'POST',
                    data: data,
                    header: {
                        'Content-Type': 'application/json'
                    },
                    success: function(res) {
                        var resData = res.data.data;
                        if(!res.data.ret){
                            var sessionId = resData.wx_session;
                            wx.setStorageSync('sessionId', resData.wx_session);
                            wx.setStorageSync('need_bind',resData.need_bind + ''); //是否需要绑定 1是 0否
                            if(resData.need_update){
                                wx.getUserInfo({
                                    success:function(res){
                                        var infoData = Object.assign(res,{wx_session:sessionId});
                                        // wx.setStorageSync('user_info',res.userInfo);
                                        wx.setStorage({
                                            key:'user_info',
                                            data:res.userInfo,
                                            success:function(res){
                                                console.log('getUserInfo success')
                                                if(isbeta) {// 测试环境
                                                  updateInfoCgi = project.domain + '/wxapp_api/api'
                                                  infoData.action = '/wxapp_api/user/update_info'
                                                }
                                                wx.request({
                                                    url: updateInfoCgi,
                                                    method: 'POST',
                                                    data: infoData,
                                                    header: {
                                                        'Content-Type': 'application/json'
                                                    },
                                                    success: function(res) {
                                                        console.log('success',res)
                                                        if(!res.ret){
                                                            console.log('信息上报成功');
                                                            resolve();
                                                        }
                                                    },
                                                    fail:function(res){
                                                        console.log('fail')
                                                        console.log(res)
                                                    },
                                                    complete:function(res){
                                                        console.log('complete',res)
                                                        resolve();
                                                    }
                                                });
                                            },
                                            fail:function(){

                                            }
                                        });
                                        
                                    },
                                    fail:function(res){
                                        console.log('getUserInfo fail',res);
                                    },
                                    complete:function(res){
                                        console.log('getUserInfo complete',res)
                                    }
                                })
                            }else{
                                resolve();
                            }
                        }else{
                            console.log(res.data.msg);
                        }
                        
                    },
                    fail:function(res) {
                        console.log(res.errMsg)
                    }
                });
            }
        },
        fail:function(res){
            console.log(res);
        }
    });
}

exports.login = function(){
    return new Promise((resolve,reject) => {
        console.log('login')
        var sessionId = wx.getStorageSync('sessionId');
        wx.checkSession({
            success:function(){
                if(!sessionId){
                    gotoLogin(resolve);
                }else{
                    resolve();
                }
                
            },
            fail:function(){
                gotoLogin(resolve);
            }
        });
    });
}
